package com.adon.ar.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class SampleView extends View {

	private Camera camera;
	private Bitmap mTexture[];
	private Matrix mMatrix = new Matrix();
	private Paint mPaint = new Paint();
	
	private int mLastMotionX, mLastMotionY;
	
	public float [] matrixValues = new float[9]; 

	// 圖片旋轉的中心位置
	private int centerX, centerY;
	// 圖片轉動的距離
	private int deltaX, deltaY;
	// 圖片長、寬
	private int width, height;
	
	private float scaleWidth, scaleHeight;

	// 螢幕長、寬
	private int display_width, display_height;

	Display display;
	private final int animationDelay = 150;
	private int mBitmapStatus = 0;
	private int mBitmapCount ;
	Handler handler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			mBitmapStatus++;
			mBitmapStatus = mBitmapStatus % mBitmapCount;
			invalidate();
			this.sendEmptyMessageDelayed(0, animationDelay);
			// super.dispatchMessage(msg);

		}
	};

	public SampleView(Context context, Bitmap[] bitmap) {
		super(context);
		setWillNotDraw(false);
		
		// setting screen size
		display = ((WindowManager) this.getContext().getSystemService(
				Context.WINDOW_SERVICE)).getDefaultDisplay();
		display_height = display.getHeight();
		display_width = display.getWidth();
		
		mTexture = bitmap;
		camera = new Camera();
		mPaint.setAntiAlias(true);
		width 	= display_width / 2;
		height 	= width;
		scaleWidth = width / 450f ;
		scaleHeight = height / 450f;
		// 分別為圖片x軸及y軸的中點 >>除以2
		centerX = width >> 1;
		centerY = height >> 1;

		mBitmapCount = mTexture.length;

	}

	public int getTextureHeight() {
		return height;
	}

	public int getTextureWidth() {
		return width;
	}

	public float getscaleWidth() {
		return scaleWidth;
	}

	public float getscaleHeight() {
		return scaleHeight;
	}
	
	void translate(int a, int b, int c) {
		camera.save();
		camera.translate(a, b, c);
		camera.getMatrix(mMatrix);
		camera.restore();
		camera.save();

		// postInvalidate();
	}

	void rotateX(float a) {

		camera.save();
		camera.rotateZ(a);
		camera.getMatrix(mMatrix);
		camera.restore();
		camera.save();

		// 以圖片中心做旋轉
		mMatrix.preTranslate(-centerX, -centerY);
		mMatrix.postTranslate(centerX, centerY);
		camera.save();

		// postInvalidate();
	}

	float[] q = new float[9];

	void rotateZ(float a, int b, int c, int d) {
		camera.save();
		camera.rotateZ(a);
		camera.translate(b, c, d);
		camera.getMatrix(mMatrix);
		camera.restore();
		if (scaleWidth!=1 || scaleHeight!=1) {
			mMatrix.postScale(scaleWidth, scaleHeight);
		}
		// 以圖片中心做旋轉
		mMatrix.preTranslate(-centerX, -centerY);
		mMatrix.postTranslate(centerX, centerY);
		camera.save();

		// postInvalidate();
	}

	void rotate(int degreeX, int degreeY) {
		deltaX += degreeX;
		deltaY += degreeY;

		camera.save();
		camera.rotateY(deltaX);
		camera.rotateX(-deltaY);
		camera.translate(0, 0, -centerX);
		camera.getMatrix(mMatrix);
		camera.restore();

		// 以圖片中心做旋轉
		mMatrix.preTranslate(-centerX, -centerY);
		mMatrix.postTranslate(centerX, centerY);
		camera.save();

		// postInvalidate();
	}

	public void refreshTimePaint() {
		this.postInvalidate();
	}

	public void removeBitmap() {
		for (int i = 0; i < mTexture.length; i++) {
			if (this.mTexture[i] != null && !this.mTexture[i].isRecycled()) {
				this.mTexture[i].recycle();
				this.mTexture[i] = null;
			}
		}

	}

	public Bitmap[] getBitmap() {
		return mTexture;
	}

	@Override
	public void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		{
			canvas.drawBitmap(mTexture[mBitmapStatus], mMatrix, mPaint);
			mMatrix.getValues(matrixValues);
//			Log.d("ken", "mBitmapStatus:" + mBitmapStatus);
		}
	}

	protected void onAttachedToWindow() {
		handler.sendEmptyMessage(0);
	};

	@Override
	protected void onDetachedFromWindow() {
		handler.removeMessages(0);
		super.onDetachedFromWindow();
	}

}
